<?php
    require_once '../config.php';
    include '_header.php';
?>

        <h1>Stats</h1>
        
        <?php
        
        if(isset($success)){
            foreach($success as $s){
                echo '<div class="msg success">'.$s.'</div>';
            }
        }
        
        if(isset($error)){
            foreach($error as $a){
                echo '<div class="msg error">'.$a.'</div>';
            }
        }
        
        ?>
        
        <div>Provide your e-mail-address and FHPI key to see statistics for your application.</div>
        
        <form name="stats" method="post" action="?action=stats">
            <input type="text" name="mail" placeholder="e-mail-address" />&nbsp;
            <input type="text" name="key" placeholder="FHPI key" />&nbsp;
            <input type="submit" name="submit" value="Show stats" />
        </form>
        <br /><br />
        <h2>Statistics</h2>
        
        <?php
            if(isset($_POST['mail']) && isset($_POST['key'])){
                $mail = strtolower(mysqli_real_escape_string($db, $_POST['mail']));
                $key = mysqli_real_escape_string($db, $_POST['key']);
                
                $data = array();
                $valid = false;
                
                // Validate Login
                $query = "SELECT * FROM users WHERE dev_mail = '$mail' AND app_key = '$key';";
                $result = mysqli_query($db, $query);
                if(mysqli_num_rows($result)==1){
                    $valid = true;
                    $query = "select service, count(*) as \"hits\" from fhpi_usage where application = '$key' AND status = \"OK\" group by (service) order by hits desc;";
                    $stats = mysqli_query($db, $query);
                    echo '<div>This is a summary of all valid hits by your application.</div><br /><table>';
                    while($row = mysqli_fetch_assoc($stats)){
                        echo '<tr><th>'.$row['service'].'</th><td>'.$row['hits'].'</td>';
                        $data['service'][] = $row['service'];
                        $data['hits'][] = $row['hits'];
                    }
                    echo '</table>';
                } else {
                    echo '<div class="msg error">Your login data is not correct.</div>';
                }
                
                if($valid){
                    echo '<br /><img src="https://chart.googleapis.com/chart?chds=a&chbh=a&chxt=x,y&cht=bvs&chd=t:';
                    for($i = 0; $i < count($data['hits']); $i++){
                        echo $data['hits'][$i];
                        if($i != (count($data['hits'])-1)){
                            echo ",";
                        }
                    }
                    echo '&chco=333333&chls=2.0&chs=549x300&chxl=0:';
                    for($i = 0; $i < count($data['service']); $i++){
                        echo '|'.$data['service'][$i];
                    }
                    echo '" alt="Stats" />';   
                }
                
                // Hits by day (line chart)
                if($valid){
                    $limit = 20;
                    unset($data);
                    $data = array();
                    $dates = array();
                    $start = time() - ($limit*24*60*60);
                    while($start < time()){
                        $dates[] = date("Y-m-d", $start);
                        $start += (24*60*60);
                    }
                    foreach($dates as $date){
                        $query = "SELECT * FROM fhpi_usage WHERE application = '$key' AND status = 'OK' AND DATE(access) = '$date';";
                        $result = mysqli_query($db, $query);
                        $data[] = mysqli_num_rows($result);
                    }
                    
                    echo '<br /><br />This are all valid hits by your application in the last '.$limit.' days, not including today.<br />';
                    echo '<img src="https://chart.googleapis.com/chart?chds=a&chbh=a&chxt=x,y&cht=lc&chd=t:';
                    for($i = 0; $i < count($data); $i++){
                        echo $data[$i];
                        if($i != (count($data)-1)){
                            echo ",";
                        }
                    }
                    echo '&chco=333333&chls=2.0&chs=549x300&chxl=0:';
                    for($i = 0; $i < count($dates); $i++){
                        echo '|'.date("d.", strtotime($dates[$i]));
                    }
                    echo '" alt="Stats" />'; 
                }
                
                
                
            }
            
        include '_footer.php';
        ?>